cc.Class({
   extends: require("GameBase"),

    properties: {
        resource1:{
            default : null,
            type : cc.SpriteFrame,
        },
		resource3:{
            default : null,
            type : cc.SpriteFrame,
        },
		resource4:{
            default : null,
            type : cc.SpriteFrame,
        },
		effectMast:{
			default:null,
			type:cc.Node
		},
		messageUi:{
			default:null,
			type:cc.Node
		},
		effectTop:{
			default:null,
			type:cc.Node
		},
		effectStar:{
			default:null,
			type:cc.Prefab
		},
		effectStarBig:{
			default:null,
			type:cc.Prefab
		},
		effectBoom:{
			default:null,
			type:cc.Prefab
		},
		canvas:{
			default:null,
			type:cc.Node
		},
		messageDisplay: {
            default: null,
            type: cc.Label
        },
		goonDisplay: {
            default: null,
            type: cc.Label
        },
		backDisplay: {
            default: null,
            type: cc.Label
        },
    },
	
	log(block){
		if(block.type1==this.targetType1&&block.type2==this.targetType2){
			this.completeAdd++;
		}
	},
	
	refreshLog(){
		this.complete+=this.completeAdd;
		this.completeAdd=0;
	},

    onLoad: function () {
		
		this.initBaseGame(3);
		
		if(this.targetType1!=10){
			
			if(this.targetType1==-1){
				this.targetSign = this.createNode(this.width/2-this.w/4-5,250+this.height/2+this.h/4,this.blockPointFrameArr[5][this.targetType2],0.5);
				this.targetSign.color = cc.color(190,190,190,255);
			}else{
				this.targetSign = this.createNode(this.width/2-this.w/4-5,250+this.height/2+this.h/4,this.blockPointFrameArr[this.targetType1][this.targetType2],0.5);
			}
		}
		else{
			this.targetSign = this.createNode(this.width/2-this.w/4-5,250+this.height/2+this.h/4,this.iceFrame,0.5);
		}
		this.node.removeChild(this.targetSign);
		this.messageUi.addChild(this.targetSign);
		
		this.hummerBtn = this.createNode(this.width/4,80,this.hummerFrame,0.8);
		this.node.removeChild(this.hummerBtn);
		this.messageUi.addChild(this.hummerBtn);
		
		this.magicBtn = this.createNode(this.width*3/4,80,this.magicFrame,0.8);
		this.node.removeChild(this.magicBtn);
		this.messageUi.addChild(this.magicBtn);
		
		this.useHummerFlag = false;
		this.useMagicFlag = false;
		
		this.hummerNmb=1;
		this.magicNmb =1;
		
    },
	
	useHummer(){
		
		this.hummerBtn.runAction(cc.sequence(cc.scaleTo(0.1,1.2,1.2),cc.scaleTo(0.1,0.8,0.8)));
		
		if(this.hummerNmb<=0)return;
		
		console.log("hummer");
		
		this.useHummerFlag = true;
		
		this.effectMast.active = true;
		
		if(this.selectedBlock!=null){
			this.node.removeChild(this.selectedMask);
			this.selectedBlock = null;
		}
		
		for(var i=0;i<this.mapPointArr.length;i++){
			
			this.node.removeChild(this.mapPointArr[i].blockNode);
			this.effectTop.addChild(this.mapPointArr[i].blockNode);
		
		}
		
		
	},
	
	useHummerOver(x,y){
		console.log("hummer over");
		
		this.useHummerFlag = false;
		
		this.effectMast.active = false;
		
		for(var i=0;i<this.mapPointArr.length;i++){
			
			this.effectTop.removeChild(this.mapPointArr[i].blockNode);
			this.node.addChild(this.mapPointArr[i].blockNode);
		
		}
		
		var mp = this.eventManager.getMapPointByPosition(x,y,this);
		
		if(mp!=null&&mp.type1<7){
			this.hummerNmb--;
			this.gameManager.pushMapPointForDisappear(mp);
			this.gameManager.disappear();
		}
		
	},
	
	useMagic(){
		
		this.magicBtn.runAction(cc.sequence(cc.scaleTo(0.1,1.2,1.2),cc.scaleTo(0.1,0.8,0.8)));
		
		if(this.magicNmb<=0)return;
		
		console.log("magic");
		
		this.useMagicFlag = true;
		
		this.effectMast.active = true;
		
		if(this.selectedBlock!=null){
			this.node.removeChild(this.selectedMask);
			this.selectedBlock = null;
		}
		
		for(var i=0;i<this.mapPointArr.length;i++){
			
			this.node.removeChild(this.mapPointArr[i].blockNode);
			this.effectTop.addChild(this.mapPointArr[i].blockNode);
		
		}
	},
	
	useMagicOver(x,y){
		console.log("magic over");
		
		var mp = this.eventManager.getMapPointByPosition(x,y,this);
		
		if(mp!=null){
			this.magicNmb--;
			mp.resetType2(8);
		}
		
		this.useMagicFlag = false;
		
		this.effectMast.active = false;
		
		for(var i=0;i<this.mapPointArr.length;i++){
			
			this.effectTop.removeChild(this.mapPointArr[i].blockNode);
			this.node.addChild(this.mapPointArr[i].blockNode);
		
		}
	},
	
	goon(){
		
		if(this.scoreadd>0){
			this.showMessage("+"+this.scoreadd);
			this.score+=this.scoreadd;
			this.scoreadd=0;
		}
		
		if(this.happyTime==0){
			if(this.gameOver()){
			
				this.effectMast.active = true;
			
				if(this.complete<this.levelTarget){
					this.showMessage("步数用完");
					this.node.runAction(cc.sequence(cc.delayTime(1.4),this.toScoreCallback));
				}else{
					this.showMessage("目标达成");
					this.happyTime=1;
					this.rewardManager.startReward();
				}
			
				return ;
			
			}else{
			
			
				if(this.mapManager.canMove(this)){
					this.selectedBlock=null;
					this.selectedBlock2=null;
				}else{
					this.moveAll();
				}
			}
		}else{
			
			this.rewardManager.disappearDuringReward();
		}
	},

});
